﻿namespace ReactionGame
{
    /// <summary>
    /// 一个测试反应速度的小游戏
    /// </summary>
    internal class Program
    {
        // 定义枚举
        enum Direction
        {
            Up, Down, Left, Right
        }
        // 定义测试次数
        const int trytime = 10;
        static void Main(string[] args)
        {
            ///设置随机数、初始时间，声明键盘输入类型
            Random rnd = new Random();
            DateTime Starttime = DateTime.Now;
            ConsoleKey input;
            int errors = 0;
            bool flag = false;
            for (int i = 0; i <= trytime; i++)
            {
                Console.Clear();
                ///利用随机数输出方位   
                int time = (int)rnd.Next(0, 4);
                Direction status = (Direction)time;
                Console.Write(status.ToString());
                ///循环判断是否输入正确   
                do
                {
                    input = Console.ReadKey(true).Key;
                    switch (status)
                    {
                        case Direction.Left:
                            if (input == ConsoleKey.LeftArrow)
                                flag = true;
                            else
                                errors++;
                            break;
                        case Direction.Right:
                            if (input == ConsoleKey.RightArrow)
                                flag = true;
                            else
                                errors++;
                            break;
                        case Direction.Up:
                            if (input == ConsoleKey.UpArrow)
                                flag = true;
                            else
                                errors++;

                            break;
                        case Direction.Down:
                            if (input == ConsoleKey.DownArrow)
                                flag = true;
                            else
                                errors++;
                            break;
                    }
                }
                while (flag == false);

            }
            Console.ReadKey();
            ///设置当前时间，计算反应时间
            DateTime Endtime = DateTime.Now;
            TimeSpan Reactiontime = Endtime - Starttime;
            Console.Clear();
            ///打印反应时间和错误个数
            if (errors < 2)
                Console.Write($"the Reactiontime is {Reactiontime},the error is {errors}");
            else
                Console.Write($"the Reactiontime is {Reactiontime},the errors are {errors}");
        }
    }
}